home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / py_compile.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  6KB  |  204 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Routine to "compile" a .py file to a .pyc (or .pyo) file.
  5.  
  6. This module has intimate knowledge of the format of .pyc files.
  7. '''
  8. import __builtin__
  9. import imp
  10. import marshal
  11. import os
  12. import sys
  13. import traceback
  14. MAGIC = imp.get_magic()
  15. __all__ = [
  16.     'compile',
  17.     'main',
  18.     'PyCompileError']
  19.  
  20. class PyCompileError(Exception):
  21.     """Exception raised when an error occurs while attempting to
  22.     compile the file.
  23.  
  24.     To raise this exception, use
  25.  
  26.         raise PyCompileError(exc_type,exc_value,file[,msg])
  27.  
  28.     where
  29.  
  30.         exc_type:   exception type to be used in error message
  31.                     type name can be accesses as class variable
  32.                     'exc_type_name'
  33.  
  34.         exc_value:  exception value to be used in error message
  35.                     can be accesses as class variable 'exc_value'
  36.  
  37.         file:       name of file being compiled to be used in error message
  38.                     can be accesses as class variable 'file'
  39.  
  40.         msg:        string message to be written as error message
  41.                     If no value is given, a default exception message will be given,
  42.                     consistent with 'standard' py_compile output.
  43.                     message (or default) can be accesses as class variable 'msg'
  44.  
  45.     """
  46.     
  47.     def __init__(self, exc_type, exc_value, file, msg = ''):
  48.         exc_type_name = exc_type.__name__
  49.         if exc_type is SyntaxError:
  50.             tbtext = ''.join(traceback.format_exception_only(exc_type, exc_value))
  51.             errmsg = tbtext.replace('File "<string>"', 'File "%s"' % file)
  52.         else:
  53.             errmsg = 'Sorry: %s: %s' % (exc_type_name, exc_value)
  54.         if not msg:
  55.             pass
  56.         Exception.__init__(self, errmsg, exc_type_name, exc_value, file)
  57.         self.exc_type_name = exc_type_name
  58.         self.exc_value = exc_value
  59.         self.file = file
  60.         if not msg:
  61.             pass
  62.         self.msg = errmsg
  63.  
  64.     
  65.     def __str__(self):
  66.         return self.msg
  67.  
  68.  
  69.  
  70. def wr_long(f, x):
  71.     '''Internal; write a 32-bit int to a file in little-endian order.'''
  72.     f.write(chr(x & 255))
  73.     f.write(chr(x >> 8 & 255))
  74.     f.write(chr(x >> 16 & 255))
  75.     f.write(chr(x >> 24 & 255))
  76.  
  77.  
  78. def compile(file, cfile = None, dfile = None, doraise = False):
  79.     """Byte-compile one Python source file to Python bytecode.
  80.  
  81.     Arguments:
  82.  
  83.     file:    source filename
  84.     cfile:   target filename; defaults to source with 'c' or 'o' appended
  85.              ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
  86.     dfile:   purported filename; defaults to source (this is the filename
  87.              that will show up in error messages)
  88.     doraise: flag indicating whether or not an exception should be
  89.              raised when a compile error is found. If an exception
  90.              occurs and this flag is set to False, a string
  91.              indicating the nature of the exception will be printed,
  92.              and the function will return to the caller. If an
  93.              exception occurs and this flag is set to True, a
  94.              PyCompileError exception will be raised.
  95.  
  96.     Note that it isn't necessary to byte-compile Python modules for
  97.     execution efficiency -- Python itself byte-compiles a module when
  98.     it is loaded, and if it can, writes out the bytecode to the
  99.     corresponding .pyc (or .pyo) file.
  100.  
  101.     However, if a Python installation is shared between users, it is a
  102.     good idea to byte-compile all modules upon installation, since
  103.     other users may not be able to write in the source directories,
  104.     and thus they won't be able to write the .pyc/.pyo file, and then
  105.     they would be byte-compiling every module each time it is loaded.
  106.     This can slow down program start-up considerably.
  107.  
  108.     See compileall.py for a script/module that uses this module to
  109.     byte-compile all installed files (or all files in selected
  110.     directories).
  111.  
  112.     """
  113.     with open(file, 'U') as f:
  114.         
  115.         try:
  116.             timestamp = long(os.fstat(f.fileno()).st_mtime)
  117.         except AttributeError:
  118.             timestamp = long(os.stat(file).st_mtime)
  119.  
  120.         codestring = f.read()
  121.     
  122.     try:
  123.         if not dfile:
  124.             pass
  125.         codeobject = __builtin__.compile(codestring, file, 'exec')
  126.     except Exception:
  127.         err = None
  128.         if not dfile:
  129.             pass
  130.         py_exc = PyCompileError(err.__class__, err, file)
  131.         if doraise:
  132.             raise py_exc
  133.         sys.stderr.write(py_exc.msg + '\n')
  134.         return None
  135.  
  136.     if cfile is None:
  137.         if not __debug__ or 'c':
  138.             pass
  139.         cfile = file + 'o'
  140.     with open(cfile, 'wb') as fc:
  141.         fc.write('\x00\x00\x00\x00')
  142.         wr_long(fc, timestamp)
  143.         marshal.dump(codeobject, fc)
  144.         fc.flush()
  145.         fc.seek(0, 0)
  146.         fc.write(MAGIC)
  147.  
  148.  
  149. def main(args = None):
  150.     """Compile several source files.
  151.  
  152.     The files named in 'args' (or on the command line, if 'args' is
  153.     not specified) are compiled and the resulting bytecode is cached
  154.     in the normal manner.  This function does not search a directory
  155.     structure to locate source files; it only compiles files named
  156.     explicitly.  If '-' is the only parameter in args, the list of
  157.     files is taken from standard input.
  158.  
  159.     """
  160.     if args is None:
  161.         args = sys.argv[1:]
  162.     rv = 0
  163.     if args == [
  164.         '-']:
  165.         while True:
  166.             filename = sys.stdin.readline()
  167.             if not filename:
  168.                 break
  169.             filename = filename.rstrip('\n')
  170.             
  171.             try:
  172.                 compile(filename, doraise = True)
  173.             continue
  174.             except PyCompileError:
  175.                 error = None
  176.                 rv = 1
  177.                 sys.stderr.write('%s\n' % error.msg)
  178.                 continue
  179.                 except IOError:
  180.                     error = None
  181.                     rv = 1
  182.                     sys.stderr.write('%s\n' % error)
  183.                     continue
  184.                 
  185.             except:
  186.                 for filename in args:
  187.                     
  188.                     try:
  189.                         compile(filename, doraise = True)
  190.                     continue
  191.                     except PyCompileError:
  192.                         error = None
  193.                         rv = 1
  194.                         sys.stderr.write(error.msg)
  195.                         continue
  196.                     
  197.  
  198.                 
  199.  
  200.     return rv
  201.  
  202. if __name__ == '__main__':
  203.     sys.exit(main())
  204.